//using MultiAgentsShared;
//using OpenAI.ObjectModels.RequestModels;

//public class ChatStepService : MonoBehavior
//{
//    private readonly ChatContextManager _chatAgentService;
//    private readonly ChatChainModel _chatChainModel;

//    private ChatStep _curChatStep;

//    public ChatStepService(ChatContextManager chatAgentService, ChatChainModel chatChainModel) : base()
//    {
//        _chatAgentService = chatAgentService;
//        _chatChainModel = chatChainModel;
//    }


//    /// <summary>
//    /// Message distribution within ChatStep, might be moved to ChatRoomService later on
//    /// </summary>
//    private void OnAssistMsgAddedToAgentHandler(ChatAgent toUpdateChatAgent, string completion, List<ToolCall> _)
//    {
//        //ignore if chatStep is not added or is already cleaned
//        if (_chatChainModel.CurChatStep == null)
//        {
//            return;
//        }

//        foreach (var chatRoom in _curChatStep.chatRooms)
//        {
//            //if in the room
//            if (chatRoom[toUpdateChatAgent.agentEnum] != null)
//            {
//                ////First chat bot will determine if ended, if ended, chat-chain will be moved to next step,
//                ////where previous msg received will be cleaned, and not processed.
//                //chatRoom.chatEndDetectionBot.AddChatRequest(message, false, 0.01f);

//                //sending completion msg of one agent or another in the room
//                foreach (var otherAgent in chatRoom.Agents)
//                {
//                    if (otherAgent.ID == toUpdateChatAgent.ID)
//                    {
//                        continue;
//                    }

//                    otherAgent.AddChatRequest(completion, ChatStringExtension.NameAsSystemReadable(toUpdateChatAgent.Name), false);
//                }
//            }
//        }
//    }

//    // TODO asd
//    //asdf
//    /// <summary>
//    /// This will only authorize moveNext if roomChat is not completed
//    /// </summary>
//    public void AuthorizeNextChat()
//    {
//        if (_curChatStep == null)
//        {
//            return;
//        }

//        foreach (var chatRoom in _curChatStep.chatRooms)
//        {
//            //Check if anyone authorized in the current room, if so continue
//            var anyoneAuthorized = chatRoom.Agents.Any(a => a.AuthorizedToMoveNext);
//            if (anyoneAuthorized)
//            {
//                continue;
//            }


//            var orderedAgents = ReorderAgentsBasedOnTalkingOrder(chatRoom, _chatChainModel.TalkingOrder);

//            foreach (var agent in orderedAgents)
//            {
//                var agentAuthorized = agent.TryAuthorize();
//                if (agentAuthorized)
//                {
//                    chatRoom.UpdateLastAuthorizedAgent(agent.agentEnum);
//                    break;
//                }
//            }
//        }
//    }



//    public void ChangeChatStep(ChatStep chatStep)
//    {
//        DebugHelper.SystemLog($"ChangeChatStep->{chatStep.chatStepPurpose}");

//        if (_curChatStep != null)
//        {
//            //Cleanup previous queuedChats also reset authroization stat;
//            foreach (var chatRoom in _curChatStep.chatRooms)
//            {
//                foreach (var agent in chatRoom.Agents)
//                {
//                    agent.PrepForNewRoom();
//                }
//            }
//        }

//        _curChatStep = chatStep;

//        //for each chatRoom of the step and for each of their agent, give system prompt of the room, who you with
//        //and your goal
//        foreach (var chatRoom in _curChatStep.chatRooms)
//        {
//            foreach (var agent in chatRoom.Agents)
//            {
//                PrepAgentForNewRoom(chatRoom, agent.agentEnum, agent);
//            }
//        }

//        //give initiator the trigger prompt
//        foreach (var chatRoom in _curChatStep.chatRooms)
//        {
//            if (chatRoom.meetingHost != ChatAgentEnum.None)
//            {
//                var initiatorMsgHost = $"@{chatRoom.meetingHost.ToString()}, you are the host of this meeting:\n" +
//                                    $"{chatRoom.meetingHostInitiator}";
//                var initiatorMsg = $"{chatRoom.meetingHostInitiator}";

//                //pass the initiatorTrigger to everyone
//                foreach (var agent in chatRoom.Agents)
//                {
//                    if (agent.agentEnum == chatRoom.meetingHost)
//                    {
//                        chatRoom[agent.agentEnum].AddChatRequest(initiatorMsgHost, "Chat_Initiator", agent.agentEnum == chatRoom.meetingHost);
//                    }
//                    else
//                    {
//                        chatRoom[agent.agentEnum].AddSystemMessage(initiatorMsg);
//                    }

//                }

//            }
//        }
//    }

//    private void PrepAgentForNewRoom(ChatRoom chatRoom, ChatAgentEnum targetAgentEnum, ChatAgent targetAgent)
//    {
//        var roomMsg =
//            $"You as {targetAgentEnum.ToString()} are moved to Step: \"{chatRoom.roomName}\" of Phase: \"{_chatChainModel.CurChatChainPhase.phaseName}\", " +
//            $"including yourself, the following agents are in the room: \n";

//        for (var i = 0; i < chatRoom.agentRequired.Count; i++)
//        {
//            var otherAgent = chatRoom.agentRequired[i];
//            roomMsg += $"{i + 1}. {otherAgent.agentEnum.ToString()}, who is responsible for {otherAgent.agentGoal}. \n";
//        }

//        switch (targetAgent.agentEnum)
//        {
//            case ChatAgentEnum.ChatEndDetectionBot:
//            case ChatAgentEnum.ChatSummaryBot:
//                break;
//            default:
//                roomMsg += "\n";
//                roomMsg += $"Goal of the current step is: {chatRoom.roomGoal}. \n";
//                targetAgent.AddSystemMessage(roomMsg);
//                break;
//        }

//    }

//    #region Helpers

//    public override void Start()
//    {
//        _chatAgentService.OnAssistMsgAddedToAgent += OnAssistMsgAddedToAgentHandler;
//    }

//    public override void OnDestroy()
//    {
//        _chatAgentService.OnAssistMsgAddedToAgent -= OnAssistMsgAddedToAgentHandler;
//    }

//    private List<ChatAgent> ReorderAgentsBasedOnTalkingOrder(ChatRoom chatRoom, TalkingOrder talkingOrder)
//    {
//        switch (talkingOrder)
//        {
//            case TalkingOrder.MsgReceivedTime:
//                return chatRoom.Agents.OrderBy(a => a.GetNextQueuedMsgReceivedTime()).ToList();
//            case TalkingOrder.InitiatorThenRoundTable:
//                List<ChatAgent> agents = new List<ChatAgent>(chatRoom.Agents);

//                // Sort using LINQ
//                var naturalSortedAgents = agents
//                    .OrderBy(a => a.agentEnum) // False (0) for priorityEnum, True (1) for others
//                    .ToList();

//                var starterIndex = -1;

//                if (chatRoom.LastAuthorizedAgent == ChatAgentEnum.None)
//                {
//                    starterIndex = naturalSortedAgents.FindIndex(a => a.agentEnum == chatRoom.meetingHost);
//                }
//                else
//                {
//                    var lastUpdatedAgentIndex = naturalSortedAgents.FindIndex(a => a.agentEnum == chatRoom.LastAuthorizedAgent);
//                    starterIndex = lastUpdatedAgentIndex + 1;
//                    if (starterIndex > (naturalSortedAgents.Count - 1))
//                    {
//                        starterIndex = 0;
//                    }
//                }

//                var starterFirstSortedAgents = ReorderListSoTargetIndexComesFirst(naturalSortedAgents, starterIndex);

//                return starterFirstSortedAgents;

//            default:
//                Debug.LogError("Undefined Talking Order");
//                return null;
//        }

//    }

//    private List<ChatAgent> ReorderListSoTargetIndexComesFirst(List<ChatAgent> list, int startIndex)
//    {
//        if (startIndex < 0 || startIndex >= list.Count)
//        {
//            throw new ArgumentException("Start index is out of range.", nameof(startIndex));
//        }

//        // Take the elements from the start index to the end, and then add the elements from the beginning to the start index
//        return list.Skip(startIndex).Concat(list.Take(startIndex)).ToList();
//    }
//    #endregion
//}
